<?php

namespace App\Console\Commands;

use App\Models\BasicInfoAnnualAssessment;
use App\Models\DictionaryItem;
use App\Models\Teachers;
use Carbon\Carbon;
use Illuminate\Console\Command;

class UpdateTeacherCurrentYearAssessment extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'UpdateCurrentYearTeacherData:assessment';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新当前年教师默认考核情况';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->info("---更新,开始:".date('Y-m-d H:i:s')."---");
        $this->taskRun();
        $this->info("---更新,结束:".date('Y-m-d H:i:s')."---");
    }

    public function taskRun()
    {
        $now = now();
        $paramDate = Carbon::create($now->year, 1, 1);
        // dd($now->toDateTimeString(), $paramDate->toDateTimeString(), $now->gt($paramDate));
        if($now->gte($paramDate)) {
            $startDate = $now->subYear()->firstOfYear()->toDateTimeString();
            $lastOfYear = $now->lastOfYear()->toDateTimeString();
            $endDate = $now->lastOfYear()->addDay()->toDateTimeString();
        } else {

            $startDate = $now->subYears(2)->firstOfYear()->toDateTimeString();
            $lastOfYear = $now->lastOfYear()->toDateTimeString();
            $endDate = $now->lastOfYear()->addDay()->toDateTimeString();
        }

        // $startOfYear = $now->firstOfYear()->toDateTimeString();
        // $lastOfYear = $now->lastOfYear()->toDateTimeString();
        // $endOfCurrentYear = $now->lastOfYear()->addDay()->toDateTimeString();
        
        $situation = DictionaryItem::select('item_order as order','id as optionId','parent_id as parentId','item_value as selectKey','item_name as selectValue')
            ->where('code', 'SITUATION')
            ->where('item_name', '合格')
            ->first();
        $noJoinSituation = DictionaryItem::select('item_order as order','id as optionId','parent_id as parentId','item_value as selectKey','item_name as selectValue')
                ->where('code', 'SITUATION')
                ->where('item_name', '不参加')
                ->first();
        $noCertainSituation = DictionaryItem::select('item_order as order','id as optionId','parent_id as parentId','item_value as selectKey','item_name as selectValue')
                ->where('code', 'SITUATION')
                ->where('item_name', '未定等次')
                ->first();

        $teachers = Teachers::where('deleted', 0)->get();
        foreach($teachers as $teacher) {
            // $userAnnualAssess = BasicInfoAnnualAssessment::where('user_id', $teacher->user_id)
            //     ->where('year', '>=', $startDate)
            //     ->where('year', '<', $endDate)
            //     ->where('deleted', 0)
            //     ->first();

            // if($userAnnualAssess) {
            //     continue;
            // } else {
            //     BasicInfoAnnualAssessment::create([
            //         'user_id' => $teacher->user_id,
            //         'situation' => json_encode([$situation->toArray()]),
            //         'year' => $lastOfYear
            //     ]);
            // }

            if(in_array($teacher->user_id, [1240, 1239, 13402, 13403])) {
                BasicInfoAnnualAssessment::updateOrcreate([
                    'user_id' => $teacher->user_id,
                    'year' => $lastOfYear
                ],[
                    'situation' => json_encode([$noJoinSituation->toArray()])
                ]);
                
                continue;
            }

            if($teacher['teaching_year'] <= 0) {
                BasicInfoAnnualAssessment::updateOrcreate([
                    'user_id' => $teacher->user_id,
                    'year' => $lastOfYear
                ],[
                    'situation' => json_encode([$noCertainSituation->toArray()])
                ]);
            } else {
                BasicInfoAnnualAssessment::updateOrcreate([
                    'user_id' => $teacher->user_id,
                    'year' => $lastOfYear
                ],[
                    'situation' => json_encode([$situation->toArray()])
                ]);
            }
        }
    }
}
